//class name = Weight
//data members = sign(Plus or Minus), grams(0 to 1000), kilograms(0 to 1000), metricTons (>=0)
/*
methods:
various contructors 
various methods for input output 
methods for add and subtract 2 instance
methods for multiply an instance with integer
methods for multiply an instance by a currency amount that is the cost per kg.
*/

public class Weight {

public static final boolean PLUS = true;
public static final boolean MINUS = false;

private boolean sign;
private short grams;
private short kilograms;
private long metricTons;

	public boolean getSign(){
	return this.sign;
	}

	public void setSign(boolean bool){
	this.sign = bool;
	}

	public short getGrams(){
	return this.grams;
	}
	public void setGrams(short grm){
		if(grm < 0 || grm > 999){
			throw new IllegalArgumentException("Grams value must be in 0 and 999"); 
		}else{
			this.grams = grm;
		}
	}
	public short getKilograms(){
		return this.kilograms;
		}
	public void setKilograms(short kg){
		if(kg < 0 || kg > 999){
			throw new IllegalArgumentException("kilograms must be in 0 and 999");
		}else {
			this.kilograms = kg;
		}
	}
	public long getMetricTons (){
		return this.metricTons;
	}
	public void setMetricTons(long mt){
		if(mt < 0){
			throw new IllegalArgumentException("MetricTon value must be greater than 0");
		}else{
			this.metricTons = mt;
		}
	}
	
	public Weight(boolean sign, short grams, short kilograms, long metricTons){
		this.sign = sign;
		
		if(grams < 0 || grams > 999){
			throw new IllegalArgumentException("Grams value must be in 0 and 999"); 
		}else {
			this.grams = grams;
		}
		
		if(kilograms < 0 || kilograms > 999){
			throw new IllegalArgumentException("kilograms must be in 0 and 999");
		}else {
			this.kilograms = kilograms;
		}
		
		if(metricTons < 0){
			throw new IllegalArgumentException("MetricTon value must be greater than 0");
		}else{
			this.metricTons = metricTons;
		}
	}
	
	public Weight(){
		this(PLUS, (short) 0, (short) 0, 0L);
	}
	public Weight(double value){
		setValue(value);
	}
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("Sign of Weight is "+ this.sign+ "\n");
		sb.append("Grams in given Weight is " + this.grams+ "\n");
		sb.append("Kilograms in given Weight is "+ this.kilograms+ "\n");
		sb.append("MetricTons in given Weight is "+ this.metricTons+ "\n");
		if(sign){
			sb.append(this.metricTons+"."+this.kilograms+this.grams);
		}else{
			sb.append("-"+this.metricTons+"."+this.kilograms+this.grams);
		}
		
		
		return sb.toString();
	}
	
	public double getValue(){
		double value =0.0;
		value = (this.metricTons*1000 + this.kilograms + this.grams/1000);
		return value;
	}
	
	public void setValue(double value){
		if(value < 0){
		 this.sign = MINUS;
		 value = -value;
		}else{
		 this.sign = PLUS;
		}
		
		this.metricTons = (long)(value + 0.0005);
		
		//taking 6 integer value or convert rest of ton into gram
		long rest = (long)((value + 0.0005 - this.metricTons) * 1000000);
		this.kilograms = (short)(rest / 1000);
		this.grams = (short)(rest - this.kilograms* 1000);		
		
	}
	
	public void setValue(Weight value){
		this.sign = value.getSign;
		this.grams = value.getGrams;
		this.kilograms = value.getKilograms;
		this.metricTons = value.getMetricTons;
	}
	
	public void input(){
		try{
		
		System.out.println("Insert weight in Kilograms");
		Scanner scan = new Scanner(System.in);
		double value = scan.nextDouble();
		setValue(value);
		}catch (InputMismatchException e){
			System.out.println("Please insert valid value");
		}
	}
	
	public long toLong(Weight weight){
		long a = weight.metricTons * 1000000 + 
					weight.kilograms * 1000  +
						weight.grams;
		if(weight.sign == MINUS){
			a = -a;
		}
		
		return a;
	}
	
	public Weight toWeight(long value){
		Weight weight = new Weight();
		if (value < 0){
			weight.sign = MINUS;
			value= - value;
		}
		weight.metricTons = (long)(value/1000000);
		weight.kilograms = (short)((value - weight.metricTons * 1000000)/1000);
		weight.grams = (short)(value - weight.metricTons*1000000 - weight.kilograms*1000);
		return weight;
	}
	
	
	public Weight add(Weight second){
		long result, first, second;
		
		first = toLong(this);
		second = toLong(second);
		
		result = first + second;
		return toWeight(result);
		
	}
	
	public Weight sub(Weight second){
		long result, first, second;
		
		first = toLong(this);
		second = toLong(second);
		
		result = first - second;
		return toWeight(result);
	}
	
	public Weight mul(int x){
		long result, first, second;
		first = toLong(this);
		result = first * x;
		return toWeight(result);
	}
	
	public static void main(String[] args){
		Weight w= new Weight(),
				x = new Weight(PLUS, (short)38, (short)740, 8L),
				 y = new Weight(-2.36598745),
				  z = new Weight(x);
				  
		System.out.println(w.toString());
	}
}


